#include "graphicspanview.h"

GraphicsPanView::GraphicsPanView(QGraphicsScene *scene, QWidget *parent)
    : QGraphicsView(scene, parent)
{
    timerID = 0;
}

void GraphicsPanView::setPanEnabled(bool enabled)
{
    if (enabled)
        timerID = startTimer(20);
    else
        killTimer(timerID);

}

void GraphicsPanView::centerView()
{
    // Set the the center point to be the center of the scene
    centerPoint.setX(scene()->width()/2);
    centerPoint.setY(scene()->height()/2);

    centerOn(centerPoint);

}

void GraphicsPanView::timerEvent(QTimerEvent *event)
{
    //If the mouse is within 10 pixles of the right edge and there is more map to the right
    if ((QCursor::pos().x() > (width() - 10)) && ((centerPoint.x() + (width()/2)) < scene()->width()))
    {
        centerPoint.setX(centerPoint.x()+10);
        centerOn(centerPoint);
        emit screenPanned(centerPoint);
    }

    //If the mouse is within 10 pixles of the left edge and there is more map to the left
    if ((QCursor::pos().x() < 10) && ((centerPoint.x() - (width()/2)) > 0))
    {
        centerPoint.setX(centerPoint.x()-10);
        centerOn(centerPoint);
        emit screenPanned(centerPoint);
    }

    //If the mouse is within 10 pixles of the top edge and there is more map to the top
    if ((QCursor::pos().y() < 10) && ((centerPoint.y() - (height()/2)) > 0))
    {
        centerPoint.setY(centerPoint.y()-10);
        centerOn(centerPoint);
        emit screenPanned(centerPoint);
    }

    //If the mouse is within 10 pixles of the bottom edge and there is more map below the edge
    if ((QCursor::pos().y() > (height() - 10)) && ((centerPoint.y() + (height()/2)) < scene()->height()))
    {
        centerPoint.setY(centerPoint.y()+10);
        centerOn(centerPoint);
        emit screenPanned(centerPoint);
    }
}

void GraphicsPanView::screenMoved(QPointF newPosition)
{
    centerPoint = newPosition;
    centerOn(centerPoint);
}

QPointF GraphicsPanView::getCenterPoint()
{
    return centerPoint;
}
